package cn.lanqiao.book.dao.impl;

import cn.lanqiao.book.dao.IBaseDao;
import cn.lanqiao.book.dao.IBookShelfDao;
import cn.lanqiao.book.pojo.ShelfBook;

import java.util.List;

public class BookShelfDaoImpl implements IBookShelfDao {

    @Override
    public int getUserId(String username) {
        String sql = "select u_id from b_user where u_username=?";
        IBaseDao IBaseDao = new IBaseDao();
        return (int) IBaseDao.queryForSingleValue(sql, username);

    }

    @Override
    public int getShelfId(int user_id, int bookshelf_type) {
        String sql = "select bs_id from b_bookshelf where u_id=? and bs_type=?";
        IBaseDao IBaseDao = new IBaseDao();
        int shelf_id;
        //下面这个异常处理解决了 书架不存在的问题
        try {
            shelf_id = (int) IBaseDao.queryForSingleValue(sql, user_id, bookshelf_type);
            return shelf_id;
        } catch (Exception e) {
            String sql2 = "insert into b_bookshelf(u_id,bs_type) values(?,?)";
            IBaseDao.update(sql2, user_id, bookshelf_type);
        }

        return (int) IBaseDao.queryForSingleValue(sql, user_id, bookshelf_type);
    }


    @Override
    public Boolean add(int shelf_id, int book_id) {
        System.out.println("过");
        String sql = "update b_bookshelf_details set bss_last_date=now() where bs_id=? and b_id=?";
        IBaseDao IBaseDao = new IBaseDao();
        System.out.println("哎");
        if (IBaseDao.update(sql, shelf_id, book_id) == 1) {
            return true;
        } else {
            String sql2 = "insert into b_bookshelf_details(bs_id,b_id) values(?,?)";
            //如果成功插入，返回true
            System.out.println("到");
            if (IBaseDao.update(sql2, shelf_id, book_id) == 1) {
                System.out.println("这");
                return true;
            } else {
                System.out.println("了");
                return false;
            }
        }
    }

    @Override
    public Boolean delete(int shelf_id, int book_id) {
        String sql = "delete from b_bookshelf_details where bs_id=? and b_id=?";
        IBaseDao IBaseDao = new IBaseDao();
        if (IBaseDao.update(sql, shelf_id, book_id) == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean add(int shelf_id, int book_id, int chapter_id) {
        String sql = "update b_bookshelf_details set bss_last_page_id=? ,bss_last_date=now() where bs_id=? and b_id=?";
            IBaseDao IBaseDao = new IBaseDao();
            if (IBaseDao.update(sql, chapter_id, shelf_id, book_id) == 1) {
                return true;
            } else {
                String sql2 = "insert into b_bookshelf_details(bs_id,b_id,bss_last_page_id) values(?,?,?)";
                //如果成功插入，返回true
                if (IBaseDao.update(sql2, shelf_id, book_id, chapter_id) == 1) {
                    return true;
                } else {
                    return false;
                }
            }


    }

    @Override
    public List<ShelfBook> getBookList(int shelfId) {
        String sql = "select t1.b_Id as b_id,t1.b_name as b_name,t1.b_author as b_author,t2.ch_Id as ch_Id,t2.ch_name as ch_name,t2.ch_time as ch_time " +
                "from  b_book t1 join " +
                "(SELECT b_chapter.* from b_chapter inner join (select  max(b_id),max(ch_id)as ch_id from b_chapter group by b_id) t on t.ch_id=b_chapter.ch_Id) t2 " +
                "on t1.b_Id=t2.b_id where t1.b_id in( select b_Id from b_bookshelf_details where bs_Id=?)";
        IBaseDao IBaseDao = new IBaseDao();
        List<ShelfBook> list = IBaseDao.queryForList(ShelfBook.class, sql, shelfId);
        return list;
    }


}
